<?php

/**
 * User Customer on Hand
 */
class UserCoH extends AppModel {

    var $name = 'UserCoH';
    var $useTable = 'user_customer_on_hands';
    var $belongsTo = array(
        'User', 'Customer'
    );
    var $validate = array(
        'user_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Please input an user id',
            'required' => true,
        ),
        'customer_id' => array(
            'rule' => 'notEmpty',
            'message' => 'Please select a customer for user',
            'required' => true,
        )
    );

    /**
     * list customer on hand ตาม user id ที่ระบุ
     * ถ้า ไม่มีข้อมูลใน ตาราง UserCustomerOnHand นี้ จะ return customers ทั้งหมด
     * @param int $user_id
     */
    function listCustomerOnHand($user_id) {
        $list = $this->find('list', array('fields' => array('customer_id'), 'conditions' => array('user_id' => $user_id)));

        if ($list == null) {
            $list = $this->Customer->find('list', array('fields' => array('id')));
        }

        $customers = array();
        foreach ($list as $k => $customer_id) {
            $customers[] = $customer_id;
        }

        return $customers;
    }
}

?>
